package com.stars.empoly.vmodel

import android.app.Activity
import android.app.Application
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.empoly.activity.ArticleDetailActivity
import com.stars.empoly.activity.MainActivity
import com.stars.empoly.model.LoginBean
import com.stars.empoly.repository.OneLoginRepository
import com.stars.empoly.utils.WeChatUtil
import com.stars.empoly.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import com.trailer.base.base.BViewModel
import com.trailer.base.ccretrofit.CCRetrofit
import com.trailer.base.route.IJpush
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch


open class OneLoginVModel(application: Application) : BViewModel<OneLoginRepository>(application) {

    var cl: ConstraintLayout? =null
    var isRead= MutableLiveData<Int>(0)//0未勾选 1已勾选


    fun toDetails(type:Int?){
        UiSwitch.bundle(repository().context, ArticleDetailActivity::class.java,
            B().putString("type",type.toString()).ok())
    }
    fun clickLogin() {


        AndPermission.with(repository().context)
                .runtime()
                .permission(Permission.WRITE_EXTERNAL_STORAGE,
                        Permission.READ_EXTERNAL_STORAGE,
                        Permission.READ_PHONE_STATE
                )
                .onGranted { data ->
                    //手机号一键登录
                    CCRetrofit.instance()
                            .with(repository().context)
                            .create(IJpush::class.java)
                            .isLogin()
                            .subscribe()

                }
                .start()
    }


    fun login(code: String) {
        viewModelScope.launch {
            var it = repository().lreq { NetEngine.service.weChatLogin(code) }
            if (it.ok()) {
                MMKV.defaultMMKV().putString("token", it.data?.token)

                UiSwitch.single(repository().context, MainActivity::class.java)
                (repository().context as Activity).finish()
            }
        }
    }

    fun wxLogin() {

        if(isRead.value==0){
            toast("请勾选")
            return
        }
        //微信登录
        WeChatUtil.setWXLoigin(repository().context)

    }



    fun oneLogin(code: String) {

        repository().rxLreq(NetEngine.service.jiguangLogin(code))
                .doOnNext() { res ->
                    if (res.ok(true)) {
                        //保存token

                        var bean: LoginBean = res.data() as LoginBean;

                        MMKV.defaultMMKV().putString("token", bean.token)
                        UiSwitch.single(repository().context, MainActivity::class.java)
                        (repository().context as Activity).finish()
                    } else {
                        if (res.code() == 0) {

                            var bean: LoginBean = res.data() as LoginBean

//                            IdentityDialogUtils.shoChooseIdentity(repository().context!!, cl!!, object : ChoiceData {
//
//                                override fun onRestuse(type: String?) {

//                                    simLogin(bean.mobile, type!!);

//                                }
//
//                            })


//                            var bean: LoginBean = res.data() as LoginBean;
//                            val items = arrayOf("司机", "货主")
//                            val builder = QMUIDialog.CheckableDialogBuilder(repository().context)
//                            builder.setTitle("选择类型去注册")
//                                    .setCheckedIndex(0)
//                                    .addItems(items) { dialog, which -> builder.checkedIndex = which }
//                                    .addAction("取消") { dialog, index ->
//                                        dialog.dismiss()
//                                    }
//                                    .addAction("确定") { dialog, index ->
//                                        var type = if (builder.checkedIndex == 0) "1" else "2"
//                                        simLogin(bean.mobile, type);
//                                        dialog.dismiss()
//                                    }
//                                    .show()


//

                        }
                    }
                }
                .subscribe()
    }

    override fun createRepository(): OneLoginRepository {
        return OneLoginRepository()
    }

}
